'use strict';

/**
 * @name 通用筛选组件
 * @description 通用筛选组件
 * @author ZYF
 */
angular.module('metalabOperationApp').factory('MetaFilter', ['MetaConstants', function(MetaConstants) {
    var metaFilter = {};

    metaFilter.dateOptions = {
        dateDisabled: function disabled(data) {
            var date = data.date,
                mode = data.mode;
            return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
        },
        formatYear: 'yy',
        maxDate: new Date(2020, 5, 22),
        minDate: new Date(2010, 5, 22),
        startingDay: 1
    };

    /**
     * @name 筛选控制参数
     * @description 所有的筛选控制参数
     */
    metaFilter.metaFilterConfig = {
        //领域
        area_choice: {
            'area01': false,
            'area02': false,
            'area03': false,
            'area04': false,
            'area05': false,
            'area06': false
        },
        //是否有诚意金
        fund: {
            'hasFund1': false,
            'hasFund2': false
        },
        //是否置顶
        rank: {
            'hasRank1': false,
            'hasRank2': false
        },
        //是否包含主页
        talent: {
            'hasTalent1': false,
            'hasTalent2': false
        },
        //省
        province: [],
        //项目经理
        manager: [],
        //需求状态, 技术状态, 进程状态
        state: [],
        //技术来源
        origin: [],
        //一级学科
        disciplineClass: '',
        //二级学科
        discipline: '',
        //交付形式
        technologyForm: '',
        //创建时间
        created: {
            dataOptions: metaFilter.dateOptions,
            startDate: new Date(),
            endDate: new Date(),
            startOpened: false,
            endOpened: false,
            startOpenFunc: function() {
                this.startOpened = true;
            },
            endOpenFunc: function() {
                this.endOpened = true;
            }
        },
        //审核时间
        verified: {
            dataOptions: metaFilter.dateOptions,
            startDate: new Date(),
            endDate: new Date(),
            startOpened: false,
            endOpened: false,
            startOpenFunc: function() {
                this.startOpened = true;
            },
            endOpenFunc: function() {
                this.endOpened = true;
            }
        }
    };

    /**
     * @name 初始化筛选模块
     * @description 先创建筛选组件，然后解析URL
     */
    metaFilter.initFilterModule = function(params, config, stateParams) {
        metaFilter.buildFilterParams(params, config);
        metaFilter.decodeURLFilterParams(params, stateParams);
    };

    /**
     * @name 创建筛选控制参数
     * @description 通用筛选组件
     */
    metaFilter.buildFilterParams = function(params, config) {
        for (var i = 0; i < config.length; i++) {
            params[config[i]] = angular.copy(metaFilter.metaFilterConfig[config[i]]);
        }
    };

    /**
     * @name 创建changeUrl的参数对象
     * @description 创建changeUrl的参数对象，即$state.go()挑战时需要用到的参数
     */
    metaFilter.encodeURLFilterParams = function(urlParams, filterParams) {
        angular.forEach(filterParams, function(value, key) {
            switch (key) {
                case 'area_choice':
                    urlParams.area = (value.area01 ? '1' : '0') + (value.area02 ? '1' : '0') + (value.area03 ? '1' : '0') + (value.area04 ? '1' : '0') + (value.area05 ? '1' : '0') + (value.area06 ? '1' : '0');
                    break;
                case 'fund':
                    urlParams.fund = (value.hasFund1 * 1 + value.hasFund2 * 2) + '';
                    break;
                case 'rank':
                    urlParams.rank = (value.hasRank1 * 1 + value.hasRank2 * 2) + '';
                    break;
                case 'talent':
                    urlParams.talent = (value.hasTalent1 * 1 + value.hasTalent2 * 2) + '';
                    break;
                case 'province':
                case 'manager':
                case 'state':
                case 'origin':
                    urlParams[key] = value.join(',');
                    break;
                case 'disciplineClass':
                case 'discipline':
                case 'technologyForm':
                    urlParams[key] = value;
                    break;
                default:
                    break;
            }
        });
    };

    /**
     * @name 对筛选控制参数进行赋值
     * @description 根据$stateParams的值，对筛选控制参数进行赋值
     */
    metaFilter.decodeURLFilterParams = function(params, stateParams) {
        angular.forEach(stateParams, function(value, key) {
            if (value !== undefined) {
                switch (key) {
                    case 'area':
                        for (var i = 0; i < (MetaConstants.map.areas.length / 2); i++) {
                            params.area_choice[MetaConstants.map.areas[(MetaConstants.map.areas.length / 2) + i]] = stateParams.area.substr(i, 1) === '1' ? true : false;
                        }
                        break;
                    case 'fund':
                        params.fund.hasFund1 = (parseInt(stateParams.fund) % 2 === 0 ? false : true);
                        params.fund.hasFund2 = (parseInt(stateParams.fund) < 2 ? false : true);
                        break;
                    case 'rank':
                        params.rank.hasRank1 = (parseInt(stateParams.rank) % 2 === 0 ? false : true);
                        params.rank.hasRank2 = (parseInt(stateParams.rank) < 2 ? false : true);
                        break;
                    case 'talent':
                        params.talent.hasTalent1 = (parseInt(stateParams.talent) % 2 === 0 ? false : true);
                        params.talent.hasTalent2 = (parseInt(stateParams.talent) < 2 ? false : true);
                        break;
                    case 'province':
                        params.province = stateParams.province.split(',');
                        break;
                    case 'manager':
                        params.manager = stateParams.manager.split(',');
                        break;
                    case 'state':
                        params.state = stateParams.state.split(',');
                        break;
                    case 'origin':
                        params.origin = stateParams.origin.split(',');
                        break;
                    case 'disciplineClass':
                        params.disciplineClass = stateParams.disciplineClass;
                        break;
                    case 'discipline':
                        params.discipline = stateParams.discipline;
                        break;
                    case 'technologyForm':
                        params.technologyForm = stateParams.technologyForm;
                        break;
                    default:
                        break;
                }
            }
        });
    };

    return metaFilter;
}]);
